package com.weitu.contractor.controller;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.contractor.entity.ManagerCert;
import com.weitu.contractor.entity.ManagerCertCriteria;
import com.weitu.contractor.entity.ManagerCertHistory;
import com.weitu.contractor.entity.ManagerCertHistoryCriteria;
import com.weitu.contractor.service.ManagerCertHistoryService;
import com.weitu.contractor.service.ManagerCertService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * @author LiMeng 2017/4/15
 */
@Controller
@RequestMapping(value = "contractor/managerCert")
public class ManagerCertController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "contractor/managerCert";
    private final ManagerCertService managerCertService;
    private final ManagerCertHistoryService managerCertHistoryService;

    @Autowired
    public ManagerCertController(ManagerCertService managerCertService, ManagerCertHistoryService managerCertHistoryService) {
        this.managerCertService = managerCertService;
        this.managerCertHistoryService = managerCertHistoryService;
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object businessListAjax(HttpServletRequest request, String sort, String personId) throws Exception {
        ManagerCertCriteria criteria = new ManagerCertCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andPersonIdEqualTo(personId);

        int count = managerCertService.countByCriteria(criteria);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);//获取分页对象
        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        //未传sort参数是采用添加时间排序
        if (StringUtils.isEmpty(sort)) {
            pageView.setSortColumn("addTime");
        } else {
            criteria.setOrderByClause(pageView.getSortColumn() + " " + pageView.getOrderType());
        }
        criteria.setPageView(pageView);

        List<ManagerCert> managerCerts = managerCertService.selectByCriteria(criteria);

        return dataGridPagination.flushDataGridData(managerCerts);
    }

    @SystemLog(module = "承包商人员", methods = "更新上传证书附件信息")
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public AjaxResult certUploadAjax(String id, String personId) throws Exception {
        if (StringUtils.isEmpty(personId)) {
            return fail("附件信息保存失败！");
        }
        ManagerCert managerCert = new ManagerCert();
        managerCert.setId(id);
        managerCert.setUpdateUserId(getSessionUser().getId());
        managerCert.setUpdateDeptId(getSessionUser().getDepartment().getId());
        managerCert.setUpdateTime(DateTimeUtil.formatterDateTime());
        managerCert.setPersonId(personId);
        if (managerCertService.updateByPrimaryKeySelective(managerCert) > 0) {
            if (!AccessoryUtil.saveAccerssoryInfo(id)) {
                return fail("附件信息保存失败！");
            }
            return success("附件信息保存成功！");
        }
        return fail("附件信息保存失败！");
    }

    @SystemLog(module = "承包商人员", methods = "更新证书信息")
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult certUpdateAjax(ManagerCert managerCert) throws Exception {
        managerCert.setUpdateUserId(getSessionUser().getId());
        managerCert.setUpdateDeptId(getSessionUser().getDepartment().getId());
        managerCert.setUpdateTime(DateTimeUtil.formatterDateTime());
        if (managerCertService.updateByPrimaryKeySelective(managerCert) > 0) {
            ManagerCertHistory history = new ManagerCertHistory();
            history.setId(IDGenerator.getId());
            history.setAddUserId(getSessionUser().getId());
            history.setAddDeptId(getSessionUser().getDepartment().getId());
            history.setAddTime(DateTimeUtil.formatterDateTime());
            history.setCertId(managerCert.getId());
            history.setPersonId(managerCert.getPersonId());
            history.setName(managerCert.getName());
            history.setCertCode(managerCert.getCertCode());
            history.setValidStartDate(managerCert.getValidStartDate());
            history.setValidEndDate(managerCert.getValidEndDate());
            managerCertHistoryService.insertSelective(history);
            return success("保存成功！");
        }
        return fail("保存失败！");
    }

    @SystemLog(module = "承包商人员", methods = "添加证书信息")
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult certSaveAjax(String id, String personId) throws Exception {
        if (StringUtils.isEmpty(personId)) {
            return fail("添加失败！");
        }
        ManagerCert managerCert = new ManagerCert();
        managerCert.setId(id);
        managerCert.setAddUserId(getSessionUser().getId());
        managerCert.setAddDeptId(getSessionUser().getDepartment().getId());
        managerCert.setAddTime(DateTimeUtil.formatterDateTime());
        managerCert.setPersonId(personId);
        if (managerCertService.insertSelective(managerCert) > 0) {
            return success("添加成功！");
        }
        return fail("添加失败！");
    }

    @SystemLog(module = "承包商人员", methods = "删除证书信息")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxResult certDeleteAjax(String id) throws Exception {
        if (managerCertService.deleteByPrimaryKey(id) > 0) {
            return success("删除成功！");
        }
        return fail("删除失败！");
    }

    @ResponseBody
    @RequestMapping(value = "/historyList", method = RequestMethod.POST)
    public Object certHistoryListAjax(HttpServletRequest request, String certId, String personId) throws Exception {
        ManagerCertHistoryCriteria criteria = new ManagerCertHistoryCriteria();
        criteria.createCriteria()
                .andDelStateEqualTo(0)
                .andPersonIdEqualTo(personId)
                .andCertIdEqualTo(certId);

        int count = managerCertHistoryService.countByCriteria(criteria);

        DataGridPagination dataGridPagination = DataGridPagination.getInstance(request, count);//获取分页对象
        PageView pageView = dataGridPagination.getPageView();//生成pageView分页对象
        criteria.setPageView(pageView);

        List<ManagerCertHistory> managerCertHistories = managerCertHistoryService.selectByCriteria(criteria);

        return dataGridPagination.flushDataGridData(managerCertHistories);
    }

    @ResponseBody
    @RequestMapping(value = "/selectByPersonId", method = RequestMethod.GET)
    public AjaxResult selectByPersonId(String personId) throws Exception {
        List<ManagerCert> managerCerts = managerCertService.selectByPersonId(personId);
        if (managerCerts.size() == 1){
            return success("数据获取成功！", managerCerts.get(0));
        }else {
            return fail("数据获取失败！");
        }
    }
}
